package org.dromara.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.PageData;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoExample1;
import org.dromara.demo.service.DemoExample1ServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 样例单表1
 *
 * @author wenrui Su
 * @date 2024-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/example1")
public class DemoExample1Controller extends BaseService {

  private final DemoExample1ServiceImpl demoExample1Service;

  /**
   * 查询样例单表1列表
   */
  @SaCheckPermission("demo:example1:list")
  @GetMapping("/list")
  public TableDataInfo<DemoExample1> list(DemoExample1 bo, PageQuery pageQuery) {
    return demoExample1Service.queryPageList(bo, pageQuery);
  }


  /**
   * 导出样例单表1列表
   */
  @SaCheckPermission("demo:example1:export")
  @Log(title = "样例单表1", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(DemoExample1 bo, HttpServletResponse response) {
    List<DemoExample1> list = demoExample1Service.queryList(bo);
    ExcelUtil.exportExcel(list, "样例单表1", DemoExample1.class, response);
  }

  /**
   * 获取样例单表1详细信息
   *
   * @param id 主键
   * @return
   */
  @SaCheckPermission("demo:example1:query")
  @GetMapping("/{id}")
  public PageData getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
//    PageData<DemoExample1> res = new PageData<>();
//    return res.success(demoExample1Service.queryById(id));
    return this.resData(demoExample1Service.queryById(id));
  }

  /**
   * 新增样例单表1
   */
  @SaCheckPermission("demo:example1:add")
  @Log(title = "样例单表1", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public PageData add(@Validated(AddGroup.class) @RequestBody DemoExample1 bo) {
    return this.resData(demoExample1Service.insertByBo(bo));
  }

  /**
   * 修改样例单表1
   */
  @SaCheckPermission("demo:example1:edit")
  @Log(title = "样例单表1", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public PageData edit(@Validated(EditGroup.class) @RequestBody DemoExample1 bo) {
    return this.resData(demoExample1Service.updateByBo(bo));
  }

  /**
   * 删除样例单表1
   *
   * @param ids 主键串
   */
  @SaCheckPermission("demo:example1:remove")
  @Log(title = "样例单表1", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public PageData remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return this.resData(demoExample1Service.deleteWithValidByIds(List.of(ids), true));
  }
}
